package com.qq.demo.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.qq.connect.ShareToken;

public class AddShareServlet extends HttpServlet {

    private static final long serialVersionUID = 8114052199656694703L;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String openid = (String) request.getSession().getAttribute("openid");
        String oauth_token = (String) request.getSession().getAttribute("oauth_token");
        String oauth_token_secret = (String) request.getSession().getAttribute("oauth_token_secret");

        Map<String, String> shares = new HashMap<String, String>();
        shares.put("title", "我开始使用B3log了，快来看看吧!" + System.currentTimeMillis());
        shares.put("url", "http://www.b3log.org/?time=" + System.currentTimeMillis());
        shares.put("images", "http://www.b3log.org/images/logo.png)?time=" + System.currentTimeMillis());
        
        ShareToken sharetoken = new ShareToken();
        String res = null;
        try {
            res = sharetoken.addShare(oauth_token, oauth_token_secret, openid, "xml", shares);
        } catch (Exception e) {
            e.printStackTrace();
        }

        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        System.out.println(res);
        out.println(res);
        out.flush();
        out.close();
    }
}
